package cn.demengsite.controller;

import cn.demengsite.dao.MemberDAO;
import cn.demengsite.dao.OrderDAO;
import cn.demengsite.dao.ReportDAO;
import cn.demengsite.model.BaseInfo;
import cn.demengsite.model.Member;
import cn.demengsite.model.Order;
import cn.demengsite.model.Report;
import cn.demengsite.service.ReportService;
import com.alibaba.fastjson.JSON;
import org.apache.camel.spi.AsEndpointUri;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.ideashare.utils.IS_ListUtils;
import tech.ideashare.utils.is_mail.IS_MailConfig;
import tech.ideashare.utils.is_mail.IS_MailUtils;
import tech.ideashare.utils.is_sms.IS_SMSConfig;
import tech.ideashare.utils.is_sms.IS_SMSUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;

@Controller
public class ReportController {

    private   final Logger logger = Logger.getLogger(MemberController.class.getName());

    @Autowired
    private ReportService reportService;

    @Autowired
    private OrderDAO orderDAO;

    @Autowired
    private MemberDAO memberDAO;

    @Autowired
    private ReportDAO reportDAO;

    @GetMapping("/report/showReportPage")
    public String showReportPage(){
        logger.info("come in report");
        return "report";
    }

    @GetMapping("/report/getSaleData/{date}")
    @ResponseBody
    public String getSaleData(@PathVariable String date){
        System.out.println(date);
        logger.info("come in report");
        BaseInfo baseInfo = reportService.getSaleReport(date);
        HashMap<String ,List> map = new HashMap<>();
        map.put("data", (List) baseInfo.getObject());
        return JSON.toJSONString(map);
    }

    @GetMapping("/report/getSaleDataWithBeginEnd/{beginDate}/{endDate}")
    @ResponseBody
    public String getSaleDataWithBeginEnd(@PathVariable String beginDate, @PathVariable String endDate){
        logger.info("come in getSaleDataWithBeginEnd");
        BaseInfo baseInfo = reportService.getSaleReportWithBeginEnd(beginDate, endDate);
        HashMap<String ,List> map = new HashMap<>();
        map.put("data", (List) baseInfo.getObject());
        return JSON.toJSONString(map);
    }

    @GetMapping("/report/contactMember/{memberId}/{orderId}")
    @ResponseBody
    public String contactMember(@PathVariable Integer memberId , @PathVariable Integer orderId){

        logger.info("come in report/contactMember");
        BaseInfo baseInfo = reportService.contactMember(memberId, orderId);
        return JSON.toJSONString(baseInfo);
    }


//    @GetMapping("/report/test")
//    @ResponseBody
//    public String testReport(){
//
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//       // String today  = format.format(new Date());
//        String today = "2017-11-15";
//        StringBuffer sb  = new StringBuffer("");
//
//        //获得今天所有的订单
//        List<Order> todayOrders = orderDAO.getOrdersByTime(today,today);
//
//        List<Report> todayDemengs = reportDAO.getSaleReportWithBeginEnd(today,today);
//        if(todayDemengs!=null && todayDemengs.size()>0){
//            //获得今天的充值总金额
//            double totalCharge = todayOrders.stream().filter(o->o.getOrderStatus()==3).mapToDouble(Order::getTotalPrice).sum();
//            //获得今天总营业额(不包含充值)
//            double totalSale = todayDemengs.stream().mapToDouble(Report::getSalePrice).sum();
//            //获得今天总件数
//            long totalCount = todayDemengs.stream().count();
//            //获取今天新办的会员
//            List<Member> members = memberDAO.getMemberByCreateTime(today,today);
//            long newMemberCount = members.stream().count();
//            //老会员回头数
//            long oldMember = todayOrders.stream().filter(o->o.getOrderStatus()!=3 && o.getMemberId()!=0).filter(IS_ListUtils.distinctByKey(Order::getMemberId)).count()-newMemberCount;
//
//            //下面是库存信息
//
//
//            sb.append(today).append("\n");
//            sb.append("新.会.员：").append(newMemberCount).append("人\n");
//            sb.append("老.会.员：").append(oldMember).append("人\n");
//            sb.append("充值：").append(totalCharge).append("元\n");
//            sb.append("总营业额（不含充值）:").append(totalCount).append("件，").append(totalSale).append("元\n");
//        }else {
//            sb.append("今天行情不好，请明天再看");
//        }
//        IS_SMSConfig config = new IS_SMSConfig();
//        config.setKey("2cfb8ee9e6fc7034f8f1");
//        config.setUid("lixiang9409");
//        config.setUrl("http://utf8.api.smschinese.cn/");
//        IS_SMSUtils.sendSms(config,"17740899943",sb.toString());
//
//        sb.append("本邮件由系统自动生成，请不要回复");
//
//        String subject = "汉南德蒙斯特销售日况"+today;
//        return sb.toString();
//    }
}
